package syncer;

import java.lang.reflect.Field;

import ent.Ent1;
import ent.Ent2;

public class ObjectContentCopyer {

	public static void copyTo(Object from, Object toRef) {
		if (from.getClass() != toRef.getClass()) {
			System.out.println("unMatched class!! from " + from.getClass()
					+ " to " + toRef.getClass());
			return;
		}
		Field[] fields = toRef.getClass().getDeclaredFields();
		for (Field f : fields) {
			f.setAccessible(true);
			try {
				f.set(toRef, f.get(from));
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Ent1 ent1 = new Ent1();
		Ent2 ent2 = new Ent2();
		ent1.ent2 = ent2;
		ent2.ent1 = ent1;

		Ent1 ent1_1 = new Ent1();
		System.out.println("ent1 = " + ent1 + " , ent1.ent2 = " + ent1.ent2
				+ " , id = " + ent1.id);
		System.out.println("ent1_1 = " + ent1_1 + " , ent1_1.ent2 = "
				+ ent1_1.ent2 + " , id = " + ent1_1.id);
		copyTo(ent1_1, ent1);
		System.out.println("ent1 = " + ent1 + " , ent1.ent2 = " + ent1.ent2
				+ " , id = " + ent1.id);
		System.out.println("-------------------------");
	}

}
